import axios from 'axios'
import md5 from 'js-md5';

class http {

  post(url, params) {
      let timestamp = new Date().getTime().toString();
      let random = Math.random().toString();
      let sign =  md5('WEKJGHDF' + random + timestamp);
      params.timestamp = timestamp;
      params.random = random;
      params.sign = sign;
      if(params.user_token){
          params.user_token = window.localStorage.getItem('o2o_token');
      }
    return new Promise((resolve, rejects) => {
      axios({
        method: 'post',
        url:'http://m.leyuanip.com/index.php?s=/api/'+ url,
        data: params,
        headers:{'Accept': 'application/json'},
        timeout: 30000
      }).then(res => {
        if (res.status === 200) {
          resolve(res.data)
        } else {
          rejects(res.data)
        }
      }).catch(err => {
        console.log(666)
        rejects(err)
        console.log('数据调用失败')
      })
    })
  };
  uploadImg(url, params) {
      let timestamp = new Date().getTime().toString();
      let random = Math.random().toString();
      let sign =  md5('WEKJGHDF' + random + timestamp);
      params.timestamp = timestamp;
      params.random = random;
      params.sign = sign;
    return new Promise((resolve, rejects) => {
      axios({
        method: 'post',
        url:'http://m.leyuanip.com/index.php?s=/api/'+ url,
        params: params,
        processData:false,
        contentType:false,
        headers: {
            'Content-Type':'multipart/form-data'
        },
        timeout: 15000
      }).then(res => {
        if (res.status === 200) {
          resolve(res.data)
        } else {
          rejects(res.data)
        }
      }).catch(err => {
        rejects(err)
      })
    })
  };
    get(url, params) {
        let timestamp = new Date().getTime().toString();
        let random = Math.random().toString();
        let sign =  md5('WEKJGHDF' + random + timestamp);
        params.timestamp = timestamp;
        params.random = random;
        params.sign = sign;
        return new Promise((resolve, rejects) => {
            axios({
                method: 'get',
                url:'http://m.leyuanip.com/index.php?s=/api/'+ url,
                params: params,
                headers: {
                    'Accept':'application/json',
                },
                timeout: 15000
            }).then(res => {
                if (res.status === 200) {
                    resolve(res.data)
                } else {
                    rejects(res.data)
                }
            }).catch(err => {
                rejects(err)
            })
        })
    }
}

export default new http()
